package com.mi.million.module.system.controller;

import com.mi.million.common.util.JwtKit;
import com.mi.million.common.util.ResMsg;
import com.mi.million.module.system.service.SystemService;
import com.mi.million.mybatis.entity.SysUser;
import com.mi.rule.auth.Public;
import com.mi.rule.resource.ResC;
import com.mi.rule.resource.ResM;
import com.mi.rule.util.RequestKit;
import com.mi.rule.util.project.MiException;
import com.mi.rule.util.project.SRS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Public
@ResC(value = "用户管理", parent = "系统配置")
@RestController
@RequestMapping("/system/user")
public class UserController {
    @Autowired
    SystemService systemService;

    @ResM("账号登录")
    @RequestMapping("/public/login")
    public SRS login(@RequestBody SRS param) throws Exception {
        return systemService.login(param);
    }

    @ResM("管理员修改自己的密码")
    @RequestMapping("/updatePassword")
    public SRS updatePassword(@RequestBody SRS param) throws Exception {
        return systemService.updatePassword(param);
    }

    @ResM("添加用户")
    @RequestMapping({"/insert"})
    public SRS insert(@RequestBody SysUser user) {
        return systemService.insertSysUser(user);
    }


    @ResM("删除用户")
    @RequestMapping({"/delete"})
    public SRS delete(@RequestBody Object[] ids) {
        return systemService.deleteSysUser(ids);
    }


    @ResM("修改用户信息")
    @RequestMapping({"/update"})
    public SRS update(@RequestBody SysUser user) {
        return systemService.updateSysUser(user);
    }


    @ResM("查询用户列表")
    @RequestMapping({"/list"})
    public SRS list(@RequestBody SysUser user) {
        return systemService.listSysUser(user);
    }


    @ResM("分页查询用户")
    @RequestMapping("/page")
    public SRS page(@RequestBody SRS param) {
        return systemService.pageSysUser(param);
    }


    @ResM("用户详情")
    @RequestMapping("/info")
    public SRS one(@RequestBody SRS param) {
        return systemService.infoSysUser(param);
    }


    @ResM("刷新登录状态")
    @RequestMapping("/public/refreshToken")
    public SRS refreshToken(@RequestBody SRS param) throws Exception {
        return systemService.refreshToken(param);
    }

    @ResM("重置用户密码")
    @RequestMapping("/resetPassword")
    public SRS resetPassword(@RequestBody SRS param) throws Exception {
        return systemService.resetPassword(param);
    }

    @ResM("检查登录状态")
    @RequestMapping("/public/checkToken")
    public SRS checkToken(@RequestBody SRS param) {
        boolean token = JwtKit.check(RequestKit.request().getHeader("token"));
        if (!token) {
            throw new MiException(ResMsg.ERROR_OUT_555);
        }
        return SRS.bySuccess();
    }

    @ResM("登录日志")
    @RequestMapping("/loginTimeline")
    public SRS loginTimeline(@RequestBody SRS param) {
        return systemService.loginTimeline(param);
    }


}
